<template>
	<view class="pageview">
		<cl-navbar autoBack placeholder  :title="$t('addressbook.friend_edit_details.title')"></cl-navbar>
		<view class="pageContent">
			<view class="cellviewGrop">
				<view class="cellview" @click="tuijian">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("addressbook.friend_edit_details.Recommended")}}</text>
					</view>
					<view class="cellview__right">
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view>
			<view class="cellviewGrop">
				<view class="cellview" @click="fenGroup">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("addressbook.friend_edit_details.group")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content u-line-1">{{friendInfo.friend_group_name}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view>
			<view class="cellviewGrop">
				<view class="cellview" @click="setBeizhu">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("addressbook.friend_edit_details.setRemarks")}}</text>
					</view>
					<view class="cellview__right">
						<text class="cellview__right__content u-line-1">{{friendInfo.friend_remark}}</text>
						<u-icon name="arrow-right" color="#ffffff"></u-icon>
					</view>
				</view>
			</view>
			<view class="cellviewGrop">
				<view class="cellview">
					<view class="cellview__left">
						<text class="cellview__left__title">{{$t("addressbook.friend_edit_details.addBlack")}}</text>
					</view>
					<view class="cellview__right">
						<u-switch v-model="friendInfo.is_black" asyncChange @change="e=>setChange('black',e)" size="20" :activeValue="true" :inactiveValue="false" inactiveColor="#121325" activeColor="#121325"></u-switch>
					</view>
				</view>
			</view>
		</view>
		<view>
			<cl-edit-pop ref="clEditPop" :title="$t('addressbook.friend_edit_details.setRemarks')" @complete="complete">
				<view style="padding:30rpx;">
					<u-input
						:customStyle="{backgroundColor:'#1E1F30',height:'110rpx',padding:'0 30rpx',borderRadius: '20rpx'}"
						fontSize="28rpx"
						color="#ffffff"
						:adjustPosition="false"
						v-model="setData.friend_nickname"
					    border="none"
						focus
					  ></u-input>
				</view>
			</cl-edit-pop>
		</view>
	</view>
</template>

<script>
	import sqliteSeesion from '@/sqlite_services/seesion.js'
	import friendsBlackApi from "@/services/api/friendsBlack.js"
	import friendsApi from "@/services/api/friends.js"
	import {
		mapState
	} from 'vuex'
	export default {
		name: 'friendEditDetails',
		data() {
			return {
				setData:{
					friend_id:0,
					friend_nickname:''
				}
			}
		},
		computed: {
			...mapState({
				friendInfo: state => state.friends.friendInfo
			}),
		},
		onLoad() {
		},
		methods: {
			fenGroup(){
				this.$Router.push({
					name: 'friend_group_manage',
					params: {
						type: 'editFriend',
						friend_id:this.friendInfo.user_id
					}
				});
			},
			//设置备注
			setBeizhu(){
				let {friend_remark,user_id,nick_name} = this.friendInfo
				this.setData = {
					friend_nickname:friend_remark,
					friend_id:user_id,
					nickname:nick_name
				}
				this.$refs.clEditPop.show()
			},
			//设置备注
			async complete(){
				try{
					let resapi = await friendsApi.editNickname(this.setData)
					if(resapi){
						this.$store.commit('updateFriendInfo',{
							friend_remark:this.setData.friend_nickname,
							user_id:this.setData.friend_id,
						})
						this.$refs.clEditPop.cancel()
					}
				}catch(e){
					//TODO handle the exception
				}
			},
			tuijian(){
				this.$Router.push({
					name: 'business_share',
					params: {
						type: 'friend_card',
						infodata: this.friendInfo
					}
				});
			},
			
			async setChange(type,val){
				switch (type){
					case 'black':
						if(!val){
							try{
								let resapi = await friendsBlackApi.undock(this.friendInfo.user_id)
								if(resapi){
									this.$store.commit('updateFriendInfo',{
										is_black:false,
										user_id:this.friendInfo.user_id,
									})
								}
							}catch(e){}
						}else{
							try{
								let resapi = await friendsBlackApi.black(this.friendInfo.user_id)
								if(resapi){
									this.$store.commit('updateFriendInfo',{
										is_black:true,
										user_id:this.friendInfo.user_id,
									})
								}
							}catch(e){}
						}
						break;
					default:
						break;
				}
			}
		},
	};
</script>

<style scoped lang="scss">
	.topInfo{
		background: #1E1F30;
		border-radius: 20rpx;
		margin: 20rpx 30rpx 0;
		padding: 30rpx;
		&__view{
			display: flex;
			gap: 10rpx;
			&__item{
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 120rpx;
				&__nickname{
					font-size: 24rpx;
					color: #FFFFFF;
					margin-top: 20rpx;
				}
			}
		}
	}
	.cellviewGrop {
		background: #1E1F30;
		border-radius: 20rpx;
		margin: 20rpx 30rpx 0;
	
		.cellview {
			display: flex;
			justify-content: space-between;
			padding: 0 30rpx;
			height: 110rpx;
	
			&__left {
				display: flex;
				align-items: center;
	
	
				&__title {
					font-size: 28rpx;
					color: #FFFFFF;
					white-space: nowrap;
				}
			}
			&__right{
				display: flex;
				align-items: center;
				margin-left: 50rpx;
				&__content{
					font-size: 28rpx;
					color: #61626e;
				}
			}
		}
	}
	.controlsBotton{
		height: 110rpx;
		text-align: center;
		line-height: 110rpx;
		background: #1E1F30;
		border-radius: 20rpx;
		margin: 20rpx 30rpx 0;
		font-size: 32rpx;
		color: #FF5252;
	}
</style>